/**
 * @author Miguel Diaz//257835
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import entity.Administrator;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

/**
 * this class handle de CRUD operations in mongo db
 * for Administrators
 */

public class AdministratorMongoCtrl {

    private DBCollection dbcollection = null;

    public AdministratorMongoCtrl(DBCollection dbcollection) {
        this.dbcollection = dbcollection;
    }

    public DBCollection getDBCollection() {
        return dbcollection;
    }

    public void create(Administrator administrator) throws PreexistingEntityException {

        DBCollection dbc = getDBCollection();
        BasicDBObject document = new BasicDBObject();
        document.put("name", administrator.getName());
        document.put("middleName", administrator.getMiddleName());
        document.put("sencondName", administrator.getSecondName());
        document.put("user", administrator.getUser());
        document.put("password", administrator.getPassword());
        document.put("status", administrator.getStatus());
        document.put("document", administrator.getDocument());
        //validations of unique fields must be inserted here!
        if (dbc.findOne(document) == null) {
            dbc.insert(document);
        } else {
            throw new PreexistingEntityException("The administrator with name "
                    + administrator.getName() + "couldn't be created. Already exist.");
        }
    }

    public void edit(Administrator oldAdministrator, Administrator newAdministrator) throws NonexistentEntityException, Exception {
        DBCollection dbc = null;
        BasicDBObject old = new BasicDBObject();
        BasicDBObject newad = new BasicDBObject();

        try {
            dbc = getDBCollection();

            old.put("name", oldAdministrator.getName());
            old.put("middleName", oldAdministrator.getMiddleName());
            old.put("sencondName", oldAdministrator.getSecondName());
            old.put("user", oldAdministrator.getUser());
            old.put("password", oldAdministrator.getPassword());
            old.put("status", oldAdministrator.getStatus());
            old.put("document", oldAdministrator.getDocument());


            newad.put("name", newAdministrator.getName());
            newad.put("middleName", newAdministrator.getMiddleName());
            newad.put("sencondName", newAdministrator.getSecondName());
            newad.put("user", newAdministrator.getUser());
            newad.put("password", newAdministrator.getPassword());
            newad.put("status", newAdministrator.getStatus());
            newad.put("document", newAdministrator.getDocument());

            dbc.update(dbc.findOne(old), newad);
        } catch (Exception ex) {
            if (ex.getLocalizedMessage().equals("can't save a null object")) {
                throw new NonexistentEntityException("The administrator with name"
                        + oldAdministrator.getName() + "couldn't be updated. May no longer exist");
            }
            throw ex;
        }
    }

    public void destroy(Administrator administrator) throws NonexistentEntityException {
        DBCollection dbc = null;
        BasicDBObject todestroy = new BasicDBObject();

        try {
            dbc = getDBCollection();
            todestroy.put("name", administrator.getName());
            todestroy.put("middleName", administrator.getMiddleName());
            todestroy.put("sencondName", administrator.getSecondName());
            todestroy.put("user", administrator.getUser());
            todestroy.put("password", administrator.getPassword());
            todestroy.put("status", administrator.getStatus());
            todestroy.put("document", administrator.getDocument());

            dbc.remove(dbc.findOne(todestroy));
        } catch (Exception ex) {
            throw new NonexistentEntityException("The administrator with name "
                    + administrator.getName() + "couldn't be deleted. May no longer exist.");

        }
    }

    public void findAdministratorEntities() {
        //specific queries are pending
        DBCollection dbc = getDBCollection();
        DBCursor cursor = dbc.find();

        // loop over the cursor and display the retrieved result
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }


    }
}
